import { TypeWidgetItem, TypeDirection } from "../type";
import { getBoundingClientRect } from "../path/until";
type TypeParamsItem = {
  resultItem: TypeWidgetItem | undefined;
  dis: number;
  tempDis: number;
};
type TypeParams = {
  LEFT: TypeParamsItem,
  RIGHT: TypeParamsItem,
  UP: TypeParamsItem,
  DOWN: TypeParamsItem,
}
/**阴影算法 */
export function queryWidgetByShadowAlgorithm(widgetList: any[], direction: TypeDirection[], widgetItem: TypeWidgetItem) {
  const currentWidget = widgetItem.ele
  const currentRect = widgetItem.rect
  const params: TypeParams = {
    LEFT: {
      resultItem: undefined,
      dis: Infinity,
      tempDis: 0
    },
    RIGHT: {
      resultItem: undefined,
      dis: Infinity,
      tempDis: 0
    },
    UP: {
      resultItem: undefined,
      dis: Infinity,
      tempDis: 0
    },
    DOWN: {
      resultItem: undefined,
      dis: Infinity,
      tempDis: 0
    },
  }
  for (let i = 0; i < widgetList.length; i++) {
    const tempWidget = widgetList[i].ele
    if (tempWidget === currentWidget) continue;
    const tempWidgetRect = widgetList[i].rect;
    if (tempWidgetRect.height == 0 || tempWidgetRect.width == 0) continue;
    for (let idx = 0; idx < direction.length; idx++) {
      const ele = direction[idx];
      const _tret = tempWidgetRect
      const _cret = currentRect
      if (ele === "LEFT" && _tret.right < _cret.centerX && _tret.left < _cret.left && _tret.top < _cret.bottom && _tret.bottom > _cret.top) {
        params[ele].tempDis = _cret.right - _tret.right;
        if (
          params[ele].tempDis < params[ele].dis ||
          (params[ele].tempDis === params[ele].dis &&
            Math.abs(_cret.centerY - _tret.centerY) <
            Math.abs(
              _cret.centerY - widgetList[i].rect.centerY
            ))
        ) {
          params[ele].resultItem = widgetList[i]
          params[ele].dis = params[ele].tempDis;
        }
      } else if (ele === "RIGHT" && _tret.left > _cret.centerX && _tret.right > _cret.right && _tret.top < _cret.bottom && _tret.bottom > _cret.top) {
        params[ele].tempDis = tempWidgetRect.left - _cret.left;
        if (
          params[ele].tempDis < params[ele].dis ||
          (params[ele].tempDis === params[ele].dis &&
            Math.abs(_cret.centerY - tempWidgetRect.centerY) <
            Math.abs(
              _cret.centerY - widgetList[i].rect.centerY
            ))
        ) {
          params[ele].resultItem = widgetList[i]
          params[ele].dis = params[ele].tempDis;
        }
      } else if (ele === "UP" && _tret.bottom < _cret.centerY && _tret.top < _cret.top && _tret.left < _cret.right && _tret.right > _cret.left) {
        params[ele].tempDis = _cret.bottom - _tret.bottom;
        if (
          params[ele].tempDis < params[ele].dis ||
          (params[ele].tempDis === params[ele].dis &&
            Math.abs(_cret.centerX - _tret.centerX) <
            Math.abs(
              _cret.centerX - widgetList[i].rect.centerX
            ))
        ) {
          params[ele].resultItem = widgetList[i]
          params[ele].dis = params[ele].tempDis;
        }
      } else if (ele === "DOWN" && _tret.top > _cret.centerY && _tret.bottom > _cret.bottom && _tret.left < _cret.right && _tret.right > _cret.left) {
        params[ele].tempDis = _tret.top - _cret.top;
        if (
          params[ele].tempDis < params[ele].dis ||
          (params[ele].tempDis === params[ele].dis &&
            Math.abs(_cret.centerX - _tret.centerX) <
            Math.abs(
              _cret.centerX - widgetList[i].rect.centerX
            ))
        ) {
          params[ele].resultItem = widgetList[i]
          params[ele].dis = params[ele].tempDis;
        }
      }
    }
  }
  return params
}


/**区域算法 */
export function queryWidgetByAreaAlgorithm(widgetList: any[], direction: TypeDirection[], widgetItem: TypeWidgetItem) {
  const currentWidget = widgetItem.ele
  const currentRect = widgetItem.rect
  const params: TypeParams = {
    LEFT: {
      resultItem: undefined,
      dis: Infinity,
      tempDis: 0
    },
    RIGHT: {
      resultItem: undefined,
      dis: Infinity,
      tempDis: 0
    },
    UP: {
      resultItem: undefined,
      dis: Infinity,
      tempDis: 0
    },
    DOWN: {
      resultItem: undefined,
      dis: Infinity,
      tempDis: 0
    },
  }
  for (let i = 0; i < widgetList.length; i++) {
    const tempWidget = widgetList[i].ele
    if (tempWidget === currentWidget) continue;
    const tempWidgetRect = getBoundingClientRect(tempWidget);
    if (tempWidgetRect.height == 0 || tempWidgetRect.width == 0) continue;
    for (let idx = 0; idx < direction.length; idx++) {
      const ele = direction[idx];

      if (ele === "LEFT" && tempWidgetRect.right <= currentRect.left) {
        params[ele].tempDis = Infinity;
        if (tempWidgetRect.bottom < currentRect.top) {
          params[ele].tempDis = Math.sqrt(
            Math.pow(currentRect.left - tempWidgetRect.right, 2) +
            Math.pow(currentRect.top - tempWidgetRect.bottom, 2)
          );
        } else if (tempWidgetRect.top > currentRect.bottom) {
          params[ele].tempDis = Math.sqrt(
            Math.pow(currentRect.left - tempWidgetRect.right, 2) +
            Math.pow(tempWidgetRect.top - currentRect.bottom, 2)
          );
        } else {
          params[ele].tempDis = currentRect.left - tempWidgetRect.right;
        }
        if (params[ele].tempDis < params[ele].dis) {
          params[ele].resultItem = widgetList[i]
          params[ele].dis = params[ele].tempDis;
        }
      } else if (ele === "RIGHT" && tempWidgetRect.left >= currentRect.right) {
        params[ele].tempDis = Infinity;
        if (tempWidgetRect.bottom < currentRect.top) {
          params[ele].tempDis = Math.sqrt(
            Math.pow(tempWidgetRect.left - currentRect.right, 2) +
            Math.pow(currentRect.top - tempWidgetRect.bottom, 2)
          );
        } else if (tempWidgetRect.top > currentRect.bottom) {
          params[ele].tempDis = Math.sqrt(
            Math.pow(tempWidgetRect.left - currentRect.right, 2) +
            Math.pow(tempWidgetRect.top - currentRect.bottom, 2)
          );
        } else {
          params[ele].tempDis = tempWidgetRect.left - currentRect.right;
        }
        if (params[ele].tempDis < params[ele].dis) {
          params[ele].resultItem = widgetList[i]
          params[ele].dis = params[ele].tempDis;
        }
      } else if (ele === "UP" && tempWidgetRect.bottom <= currentRect.top) {
        params[ele].tempDis = Infinity;
        if (tempWidgetRect.right < currentRect.left) {
          params[ele].tempDis = Math.sqrt(
            Math.pow(currentRect.top - tempWidgetRect.bottom, 2) +
            Math.pow(currentRect.left - tempWidgetRect.right, 2)
          );
        } else if (tempWidgetRect.left > currentRect.right) {
          params[ele].tempDis = Math.sqrt(
            Math.pow(currentRect.top - tempWidgetRect.bottom, 2) +
            Math.pow(tempWidgetRect.left - currentRect.right, 2)
          );
        } else {
          params[ele].tempDis = currentRect.top - tempWidgetRect.bottom;
        }
        if (params[ele].tempDis < params[ele].dis) {
          params[ele].resultItem = widgetList[i]
          params[ele].dis = params[ele].tempDis;
        }
      } else if (ele === "DOWN" && tempWidgetRect.top >= currentRect.bottom) {
        params[ele].tempDis = Infinity;
        if (tempWidgetRect.right < currentRect.left) {
          params[ele].tempDis = Math.sqrt(
            Math.pow(tempWidgetRect.top - currentRect.bottom, 2) +
            Math.pow(currentRect.left - tempWidgetRect.right, 2)
          );
        } else if (tempWidgetRect.left > currentRect.right) {
          params[ele].tempDis = Math.sqrt(
            Math.pow(tempWidgetRect.top - currentRect.bottom, 2) +
            Math.pow(tempWidgetRect.left - currentRect.right, 2)
          );
        } else {
          params[ele].tempDis = tempWidgetRect.top - currentRect.bottom;
        }
        if (params[ele].tempDis < params[ele].dis) {
          params[ele].resultItem = widgetList[i]
          params[ele].dis = params[ele].tempDis;
        }
      }

    }
  }
  return params
}